home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / SRCHLIST < prev    next >
Text File  |  1986-12-29  |  3KB  |  103 lines

  1. /* The srchlist command finds all occurences of a pattern in a file, and */
  2. /* displays all lines which contain that pattern in another window. */
  3.  
  4. init()
  5. {
  6.   assign_key("srchlist", 19);   /* CTRL-S */
  7. }
  8.  
  9.  
  10. srchlist()
  11. {
  12.   string pattern;               /* pattern to search for */
  13.   string foo, line;
  14.   int temp_buf, old_buf;        /* buffer ids */
  15.   int found;
  16.  
  17.   found = 0;                    /* boolean to record if we found any matches */
  18.   old_buf = currbuf();
  19.   temp_buf = create_buffer("$$$list$");   /* create a new buf to hold matches */
  20.  
  21.   /* Go to the beginning of the file in order to start the matching. */
  22.   save_position();
  23.   gobof();
  24.  
  25.   /* Prompt the user for the pattern to search for. */
  26.   pattern = get_tty_str("Pattern : ");
  27.   if (strlen(pattern) < 1)  return;
  28.  
  29.   while (fsearch(pattern))
  30.   {
  31.     found = found + 1;          /* found a match!!! */
  32.     line = sprintf("%d: %s\n", currlinenum(), currline());
  33.  
  34.     /* Insert the matched line (with its line number) in the temp buffer */
  35.     setcurrbuf(temp_buf);
  36.     goeof();
  37.     insert(line);
  38.  
  39.     /* Go back to the file and resume matching at the next line. */
  40.     setcurrbuf(old_buf);
  41.     if (!down())  break;
  42.     gobol();
  43.   }
  44.  
  45.   if (found == 0)
  46.     foo = get_tty_str("Pattern not found");
  47.   else
  48.     interact(temp_buf, old_buf);
  49.  
  50.   /* Get rid of the temp buffer and go back to the file */
  51.   delete_buffer(temp_buf);
  52.   show_buffer(old_buf);
  53.   clear_mark();         /* remove all line marks in the buffer */
  54.   restore_position();
  55. }
  56.  
  57.  
  58. /* This is a nice little touch. The user can move up and down the temp buffer, */
  59. /* and when the user hits <RETURN>, the corresponding line in the original */
  60. /* file will be highlighted. */
  61. interact(new_id, old_id)
  62.   int new_id;
  63.   int old_id;
  64. {
  65.   int c, n;
  66.  
  67.   /* Get rid of the blank last line, and move to the first line. */
  68.   show_buffer(new_id);
  69.   goeof();
  70.   delline();
  71.   gobof();
  72.   show_line(new_id, old_id);
  73.  
  74.   while ((c = get_tty_char()) != '\E')          /* <ESC> ends it all */
  75.   {
  76.     if (c == 200)               /* <UP> */
  77.       up();
  78.     else if (c == 208)          /* <DOWN> */
  79.       down();
  80.     else if (c == '\n')         /* We picked a line to view */
  81.       show_line(new_id, old_id);
  82.   }
  83. }
  84.  
  85.  
  86. /* This routine goes to the old file, and highlights the line you picked. */
  87. show_line(new_id, old_id)
  88.   int new_id;
  89.   int old_id;
  90. {
  91.   int n;
  92.  
  93.   n = atoi(currline());         /* extract the line number of the line */
  94.   if (n > 0)
  95.   {
  96.     show_buffer(old_id);        /* Go to the original file */
  97.     goline(n);                  /*  and move to the desired line. */
  98.     markline();                 /* Highlight the line */
  99.     show_buffer(new_id);        /* Go back to the temp window. */
  100.     goline(currlinenum());      /* (a way of doing a refresh on the window) */
  101.   }
  102. }
  103.